# coding:utf-8
import web
import MySQLdb
import httplib,sys,os
import parserTpl
import PanelUtil 
class news:
  def GET(self, name):
    #os.system("python parserBaiDuNews.py")
    conn=MySQLdb.connect(host="10.241.84.65",user="root",db="stock")
    #conn.set_character_set('utf8')
    cursor = conn.cursor()  
    sql = "select * from stock_news"
    cursor.execute(sql)
    alldata=cursor.fetchall()
    res=dict()
    for i in alldata:
      if i[0] not in res:
        res[i[0]] = list()
      res[i[0]].append(i)
    html = ""  
    html+="<div style='height:480px'><div class='topnewsbox'><ul class='newslist'>"
    for i in res["最新"]:
      html += "<li><a href='%s'>%s</a><span>%s</span></li>" % (i[1], i[2], i[3])
    html+="</ul></div>"
    html += "<div class='newsstocktab'><ul><li id='hushi' class='focus'>沪市</li><li id='shenshi'>深市</li><li id='ganggu'>全球股市</li></ul></div>"
    html += """<script type='text/javascript'>
function clearFocus(){
$('div.newsstocktab ul li').removeClass('focus');
$("div.newsstocktab ul li").css({"background-color":"#F0F0F0"})
}
$("li#hushi").click(function(){
  if($("li#hushi").hasClass('focus')){
    return;
  }
  clearFocus();
  $(this).addClass('focus');
  $("div.newsstocktab ul li.focus").css({"background-color":"#FFFFFF"})
  $("#stockflash").attr('src', '/stockpaneltiny.do?c=000001&m=1');
})
$("li#shenshi").click(function(){
  if($("li#shenshi").hasClass('focus')){
    return;
  }
  clearFocus();
  $(this).addClass('focus');
  $("div.newsstocktab ul li.focus").css({"background-color":"#FFFFFF"})
  $("#stockflash").attr('src', '/stockpaneltiny.do?c=399001&m=2');
})
$("li#quanqiu").click(function(){
  if($("li#quanqiu").hasclass('focus')){
    return;
  }
})
</script>"""  
    html += "<div>"  
    html += "<iframe id='stockflash' frameborder='0' style='margin-bottom:-40px;width:500px;height:370px;margin-left:30px;margin-right:auto' src='/stockpaneltiny.do?c=000001&m=1'></iframe>"
    html += "</div>"    
    html += "</div>"    

    html+="<div class='newsbox'><h2>大盘</h2><ul class='newslist'>"
    for i in res["大盘"]:
      html += "<li><a href='%s'>%s</a><span>%s</span></li>" % (i[1], i[2], i[3])
    html+="</ul></div>"

    html+="<div class='newsbox'><h2>个股</h2><ul class='newslist'>"
    for i in res["个股"]:
      html += "<li><a href='%s'>%s</a><span>%s</span></li>" % (i[1], i[2], i[3])
    html+="</ul></div>"
   
    html+="<div class='newsbox'><h2>热点板块</h2><ul class='newslist'>"
    for i in res["热点板块"]:
      html += "<li><a href='%s'>%s</a><span>%s</span></li>" % (i[1], i[2], i[3])
    html+="</ul></div>"

    html+="<div class='newsbox'><h2>新股</h2><ul class='newslist'>"
    for i in res["新股"]:
      html += "<li><a href='%s'>%s</a><span>%s</span></li>" % (i[1], i[2], i[3])
    html+="</ul></div>"
  
    dictData={"content":html}
    html = parserTpl.parserTpl(dictData, 'template/menu.tpl')
    return html
n=news()
data=n.GET("")
f=open("index.html","wr")
f.write(data)
f.close()
print "create index succ"
